package org.grow.took.service;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: xwg
 * @CreateDate: 2021/9/30
 */

@Service
public class AuthFail implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        AuthenticationException e)
            throws IOException, ServletException {
        System.out.println("登录失败"+e.getClass()+e.getMessage());
//            if (e instanceof )
        if (e instanceof BadCredentialsException){
            httpServletResponse.setStatus(701);
            httpServletResponse.getWriter().println(":701:"+e.getMessage());
        }else if(e instanceof DisabledException){
            httpServletResponse.setStatus(702);
            httpServletResponse.getWriter().println(":702:"+e.getMessage());
        }else {
            httpServletResponse.setStatus(709);
            httpServletResponse.getWriter().println(":709:"+e.getMessage());
        }



    }
}
